﻿using System;
using System.Collections.Generic;
using Gp.Scripts.GUI;

namespace Gp.Scripts.Core
{
    public static class SkillEntityUtils
    {
        
        public static AttackActionResult AutoCalResult(this SkillEntity self, BaseUnit unit) => AtkAction(self, unit);

        public static AttackActionResult AutoCalResult(this SkillEntity self, BaseUnit unit, ModifierValue bonus)
        {
            return AtkAction(self, unit, e => e.BonusDamage(bonus));
        }

        public static AttackActionRequest AtkActionEntity(this SkillEntity self, BaseUnit target, float defencePercent = 0)
        {
            return new AttackActionRequest(self.UnitReference, target, self, null,
                defencePercent);
        }

        
        public static IActionResult StartAction<T>(this SkillEntity self, T entity, IEnumerable<WeakRuleWrapper<T>> rules = null)
            where T : class, IActionRequest
        {
            return Global.Get<BattleSystem>().RulesModel.HandleAction(entity, rules);
        }

        public static AttackActionResult AtkAction(this SkillEntity self,BaseUnit target, float defencePercent = 0)
        {
            return StartAction(self, AtkActionEntity(self, target, defencePercent)) as AttackActionResult;
        }

        public static AttackActionResult AtkAction(this SkillEntity self, BaseUnit target, int damage, float defencePercent = 0)
        {
            var entity = AtkActionEntity(self, target, defencePercent);
            entity.Damage = damage;
            return StartAction(self, entity) as AttackActionResult;
        }

        public static AttackActionResult AtkAction(this SkillEntity self, BaseUnit target, ModifierValue bonus, float defencePercent = 0)
        {
            var entity = AtkActionEntity(self, target, defencePercent);
            entity.BonusDamage(bonus);
            return StartAction(self, entity) as AttackActionResult;
        }


        public static AttackActionResult AtkAction(this SkillEntity self, BaseUnit target, Action<AttackActionRequest> preProcess,
            float                                                   defencePercent = 0)
        {
            var entity = AtkActionEntity(self, target, defencePercent);
            preProcess?.Invoke(entity);
            return self.StartAction(entity) as AttackActionResult;
        }


        public static AttackActionResult AtkAction(this SkillEntity self, BaseUnit target, Action<AttackActionRequest> preProcess,
            AttackPreReaction                                       args)
        {
            var entity = new AttackActionRequest(self.UnitReference, target, self, self.UnitReference.ComEquip.CurWeapon,
                args);
            preProcess?.Invoke(entity);
            return StartAction(self, entity) as AttackActionResult;
        }

        
        public static void FloatText(this SkillEntity self, BaseUnit unit, string text)
        {
            Global.Get<GridBattleGUISystem>().PlayFloatingText(unit, text);
        }

        public static void FloatText(this SkillEntity self, string text)
        {
            Global.Get<GridBattleGUISystem>().PlayFloatingText(self.UnitReference, text);
        }
    }
}